﻿using Chat.Common;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatServerLib
{
    /// <summary>
    /// 群发消息
    /// </summary>
    public class ONE2ALL : CommandBase<ChatSession, StringRequestInfo>
    {

        public override void ExecuteCommand(ChatSession session, StringRequestInfo requestInfo)
        {
            string body = requestInfo.Body;
            try
            {
                body = Base64.Decode(body);
            }
            catch { }
            session.Logger.DebugFormat("Got command: {0}=>{1}", requestInfo.Key, body);

            ResultStatus rs = new ResultStatus(ResultType.SEND);

            if (string.IsNullOrWhiteSpace(session.UserName))
            {
                rs.Success = false;
                rs.Message = "You are not JOIN yet.";
                session.Send(rs.ToString());
                return;
            }

            rs.Type = ResultType.RECEIVE;
            var sessions = session.AppServer.GetSessions(_ => !string.IsNullOrWhiteSpace(_.UserName) && _.SessionID != session.SessionID);


            rs.Message = string.Format("[{0}] {1}", session.UserName, body);

            string msg = rs.ToString();
            foreach (var item in sessions)
            {
                item.Send(msg);
            }
        }
    }
}
